package com.start.workflow.admin;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.dataRule.StartQueryWrapper;
import com.start.common.exceptions.StartBusException;
import com.start.core.annotations.RegisterPermission;
import com.start.form.engine.core.StartFormEngine;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.model.StartFormConfigModel;
import com.start.workflow.core.StartActFormType;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "流程表单数据管理")
@RestController
@RequestMapping("/start/actFormData")
public class StartActFormDataController {

    @Autowired
    private StartFormEngine formEngine;

    @GetMapping("/page/{tableId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:info')")
    public Result<?> page(@PathVariable("tableId") String tableId, Page<StartFormConfig> page, StartFormConfig entity) {
        StartQueryWrapper startQueryWrapper = DataQueryConstructor.createStartQueryWrapper();
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        return Result.ok(formEngine.getTableDataEngineService(startFormConfigModel).page(startFormConfigModel, page, startQueryWrapper));
    }

    @GetMapping("/list/{tableId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:info')")
    public Result<?> list(@PathVariable("tableId") String tableId) {
        StartQueryWrapper startQueryWrapper = DataQueryConstructor.createStartQueryWrapper();
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        return Result.ok(formEngine.getTableDataEngineService(startFormConfigModel).list(startFormConfigModel, startQueryWrapper));
    }

    private StartFormConfigModel getStartFormConfigModel(String tableId) {
        StartFormConfigModel form = formEngine.getForm(tableId);
        if (form == null) {
            throw new StartBusException("未知表单");
        }
        if (form.getFormCategory() == null) {
            throw new StartBusException("未知表单类别");
        }
        if (!form.getFormCategory().startsWith(StartActFormType.START_ACT_FROM_PREFIX.getValue())) {
            throw new StartBusException("表单类别错误");
        }
        return form;
    }

   @Operation(summary = "流程表单-添加数据")
    @PostMapping("/add/{tableId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:add')")
    @RegisterPermission(value = "start:act:form:data:add", name = "添加")
    public Result<?> add(@PathVariable("tableId") String tableId, @RequestBody JSONObject entity) {
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        return Result.ok(formEngine.getTableDataEngineService(startFormConfigModel).save(startFormConfigModel, entity));
    }

   @Operation(summary = "流程表单-修改数据")
    @PutMapping("/updateById/{tableId}/{dataId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:edit')")
    @RegisterPermission(value = "start:act:form:data:edit", name = "编辑")
    public Result<?> updateById(@PathVariable("tableId") String tableId, @PathVariable("dataId") String dataId, @RequestBody JSONObject entity) {
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        formEngine.getTableDataEngineService(startFormConfigModel).updateById(startFormConfigModel, dataId, entity);
        return Result.ok();
    }

   @Operation(summary = "流程表单-获取数据")
    @GetMapping("/getById/{tableId}/{dataId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:info')")
    @RegisterPermission(value = "start:act:form:data:info", name = "查看")
    public Result<?> getById(@PathVariable("tableId") String tableId, @PathVariable("dataId") String dataId) {
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        return Result.ok(formEngine.getTableDataEngineService(startFormConfigModel).getById(startFormConfigModel, dataId));
    }

   @Operation(summary = "流程表单-删除数据")
    @DeleteMapping("/deleteById/{tableId}/{dataId}")
    @PreAuthorize("@pas.hasPermission('start:act:form:data:delete')")
    @RegisterPermission(value = "start:act:form:data:delete", name = "删除")
    public Result<?> deleteById(@PathVariable("tableId") String tableId, @PathVariable("dataId") String dataId) {
        StartFormConfigModel startFormConfigModel = getStartFormConfigModel(tableId);
        formEngine.getTableDataEngineService(startFormConfigModel).removeById(startFormConfigModel, dataId);
        return Result.ok();
    }


}
